package com.example.spring_java_oj.controller;

import com.example.spring_java_oj.mapper.ProblemRedis;
import com.example.spring_java_oj.model.CompileResponse;
import com.example.spring_java_oj.model.Problem;
import com.example.spring_java_oj.model.Result;
import com.example.spring_java_oj.service.ProblemAmountService;
import com.example.spring_java_oj.service.UserInfoAmountService;
import com.example.spring_java_oj.service.problem.ProblemRedisService;
import com.example.spring_java_oj.service.problem.ProblemService;
import com.example.spring_java_oj.service.userInfo.UserInfoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/compile")
@Slf4j
public class CompileController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ProblemService problemService;

    @Autowired
    private ProblemRedisService problemRedis;

    @RequestMapping("/run")
    public Result run(Integer id, String code, HttpServletRequest request) throws JsonProcessingException {
        log.info("id为: "+id + " 代码为code" + code);
        //problemService.updateTemCode(id, code);
        CompileResponse response = userInfoService.run(id, code, request);
        Problem problem = problemService.getProblem(id, request);
        //Problem problem = problemAmountService.getProblem(id, request);
        System.out.println(problem.toString());
        String str1 = response.getStdout();
        String str2 = problem.getAnswer() + "\r\n";
        System.out.println(str1);
        System.out.println(str2);
        if(response.getStdout() != null && str2.equals(str1)){
            problem.setFinish(1);
//            problemAmountService.updateProblemAnswer(problem);
            problemService.updateProblemAnswer(problem);
            problemRedis.updateAndInsertProblem(problem);
            response.setStdout("恭喜，完成测试");
        }
        System.out.println(response.toString());
        Result<CompileResponse> result = Result.success(response);
        //System.out.println(result.toString());
        log.info(result.toString());
        return result;
    }


}
